package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerAlllotteryActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.AlllotteryActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResBanner;
import com.kucai.yifaqapp.presenter.activity.AlllotteryActivityPresenter;
import com.kucai.yifaqapp.ui.view.AlllotteryActivityView;
import com.kucai.yifaqapp.util.EventBus.GameDetailConstances;
import com.kucai.yifaqapp.util.OnItemClickListener;

import java.util.ArrayList;

import static com.kucai.yifaqapp.global.App.getContext;

//所有彩种
public class AlllotteryActivity extends BaseActivity<AlllotteryActivityPresenter> implements AlllotteryActivityView {

    TextView titleLeftTv;
    LinearLayout skinbg;
    TextView headerTitle;
    RecyclerView allgameList;

    private ArrayList<ResBanner.DataBeanX.GamesBean> gamedata = new ArrayList<>();//左侧游戏
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.getbanner();
        titleLeftTv = findViewById(R.id.title_left_tv);
        skinbg = findViewById(R.id.skinbg);
        headerTitle = findViewById(R.id.headerTitle);
        allgameList = findViewById(R.id.allgame_list);
        headerTitle.setText("全部彩票");
        titleLeftTv.setText("返回");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //禁止滑动
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        };
        //添加布局管理器
        allgameList.setLayoutManager(gridLayoutManager);
        allgameList.setHasFixedSize(true);
        adapter = new Adapter();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_alllottery;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }


    @Override
    protected void initDagger() {
        DaggerAlllotteryActivityComponent.builder()
                .alllotteryActivityModule(new AlllotteryActivityModule(this))
                .build().inject(this);
    }

    @Override
    public void getBanner(ResBanner resBanner) {
        if (resBanner.code == 0 && null != resBanner.data) {
            //左侧游戏
            if (resBanner.data.games != null) {
                gamedata.clear();
                for (int i = 0; i < resBanner.data.games.size(); i++) {
                    if (!resBanner.data.games.get(i).game_name.equals("棋牌游戏")) {
                        if (resBanner.data.games.get(i).game_id == 80 || resBanner.data.games.get(i).game_id == 50 ||
                                resBanner.data.games.get(i).game_id == 82 || resBanner.data.games.get(i).game_id == 99 ||
                                resBanner.data.games.get(i).game_id == 81 || resBanner.data.games.get(i).game_id == 1 ||
                                resBanner.data.games.get(i).game_id == 4 || resBanner.data.games.get(i).game_id == 91 ||
                                resBanner.data.games.get(i).game_id == 90 || resBanner.data.games.get(i).game_id == 66 ||
                                resBanner.data.games.get(i).game_id == 65 || resBanner.data.games.get(i).game_id == 86 ||
                                resBanner.data.games.get(i).game_id == 10 || resBanner.data.games.get(i).game_id == 12 ||
                                resBanner.data.games.get(i).game_id == 13 || resBanner.data.games.get(i).game_id == 16 ||
                                resBanner.data.games.get(i).game_id == 15 || resBanner.data.games.get(i).game_id == 18 ||
                                resBanner.data.games.get(i).game_id == 11 || resBanner.data.games.get(i).game_id == 60 ||
                                resBanner.data.games.get(i).game_id == 61 || resBanner.data.games.get(i).game_id == 21 ||
                                resBanner.data.games.get(i).game_id == 70 || resBanner.data.games.get(i).game_id == 85 ||
                                resBanner.data.games.get(i).game_id == 113 || resBanner.data.games.get(i).game_id == 801
                                || resBanner.data.games.get(i).game_id == 802 || resBanner.data.games.get(i).game_id == 803
                                || resBanner.data.games.get(i).game_id == 804 || resBanner.data.games.get(i).game_id == 901
                                || resBanner.data.games.get(i).game_id == 902 || resBanner.data.games.get(i).game_id == 903
                                || resBanner.data.games.get(i).game_id == 904 || resBanner.data.games.get(i).game_id == 55
                                ) {
                            gamedata.add(resBanner.data.games.get(i));
                        }
                    }
                }
                allgameList.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.game_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            holder.geme_tv.setText(gamedata.get(position).game_name);
            int sre = gamedata.get(position).game_id;


            holder.itemView.setVisibility(View.VISIBLE);
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

            switch (sre) {
                case 80:
                    Glide.with(getContext()).load(R.mipmap.mssc).into(holder.game_iv);
                    break;
                case 50:
                    Glide.with(getContext()).load(R.mipmap.bjsc).into(holder.game_iv);
                    break;
                case 82:
                    Glide.with(getContext()).load(R.mipmap.msft).into(holder.game_iv);
                    break;
                case 99:
                    Glide.with(getContext()).load(R.mipmap.xgpm).into(holder.game_iv);
                    break;
                case 70:
                    Glide.with(getContext()).load(R.mipmap.xglhc).into(holder.game_iv);
                    break;
                case 81:
                    Glide.with(getContext()).load(R.mipmap.msssc).into(holder.game_iv);
                    break;
                case 1:
                    Glide.with(getContext()).load(R.mipmap.cqssc).into(holder.game_iv);
                    break;
                case 4:
                    Glide.with(getContext()).load(R.mipmap.xjssc).into(holder.game_iv);
                    break;
                case 60:
                    Glide.with(getContext()).load(R.mipmap.gdklsf).into(holder.game_iv);
                    break;
                case 91:
                    Glide.with(getContext()).load(R.mipmap.msnn).into(holder.game_iv);
                    break;
                case 90:
                    Glide.with(getContext()).load(R.mipmap.pksnn).into(holder.game_iv);
                    break;
                case 61:
                    Glide.with(getContext()).load(R.mipmap.cqxync).into(holder.game_iv);
                    break;
                case 66:
                    Glide.with(getContext()).load(R.mipmap.pcdd).into(holder.game_iv);
                    break;
                case 65:
                    Glide.with(getContext()).load(R.mipmap.bjklb).into(holder.game_iv);
                    break;
                case 86:
                    Glide.with(getContext()).load(R.mipmap.msks).into(holder.game_iv);
                    break;
                case 85:
                    Glide.with(getContext()).load(R.mipmap.xylhc).into(holder.game_iv);
                    break;
                case 10:
                    Glide.with(getContext()).load(R.mipmap.jsks).into(holder.game_iv);
                    break;
                case 12:
                    Glide.with(getContext()).load(R.mipmap.gxks).into(holder.game_iv);
                    break;
                case 13:
                    Glide.with(getContext()).load(R.mipmap.hubks).into(holder.game_iv);
                    break;
                case 11:
                    Glide.with(getContext()).load(R.mipmap.ahks).into(holder.game_iv);
                    break;
                case 16:
                    Glide.with(getContext()).load(R.mipmap.gsks).into(holder.game_iv);
                    break;
                case 15:
                    Glide.with(getContext()).load(R.mipmap.hbks).into(holder.game_iv);
                    break;
                case 18:
                    Glide.with(getContext()).load(R.mipmap.gzks).into(holder.game_iv);
                    break;
                case 21:
                    Glide.with(getContext()).load(R.mipmap.gdsyxw).into(holder.game_iv);
                    break;
                case 112:
                    //腾讯
                    Glide.with(getContext()).load(R.mipmap.txffc).into(holder.game_iv);
                    break;
                case 113:
                    Glide.with(getContext()).load(R.mipmap.qqffc).into(holder.game_iv);
                    break;
                //快速赛车
                case 801:
                    Glide.with(getContext()).load(R.mipmap.kssc).into(holder.game_iv);
                    break;
                //快速飞艇
                case 802:
                    Glide.with(getContext()).load(R.mipmap.ksft).into(holder.game_iv);
                    break;
                //快速时时彩
                case 803:
                    Glide.with(getContext()).load(R.mipmap.ksssc).into(holder.game_iv);
                    break;
                //幸运feiting
                case 804:
                    Glide.with(getContext()).load(R.mipmap.twxyft).into(holder.game_iv);
                    break;
                case 901:
                    Glide.with(getContext()).load(R.mipmap.sfsc).into(holder.game_iv);
                    break;
                //快速飞艇
                case 902:
                    Glide.with(getContext()).load(R.mipmap.sfssc).into(holder.game_iv);
                    break;
                //快速时时彩
                case 903:
                    Glide.with(getContext()).load(R.mipmap.jslhc).into(holder.game_iv);
                    break;
                case 904:
                    Glide.with(getContext()).load(R.mipmap.sflhc).into(holder.game_iv);
                    break;
                case 55:
                    Glide.with(getContext()).load(R.mipmap.xyft).into(holder.game_iv);
                    break;
                default:
                    //如果以上没有设置，那么隐藏
                    holder.itemView.setVisibility(View.GONE);

                    break;
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), GameDetailActivityFinal.class);
                    GameDetailConstances.GameId = gamedata.get(position).game_id;
                    GameDetailConstances.GameName = gamedata.get(position).game_name + "";
                    intent.putExtra("gameid", gamedata.get(position).game_id + "");
                    intent.putExtra("gamename", gamedata.get(position).game_name + "");
                    if (null != Info.user) {
                        startActivity(intent);
                        finish();
                    } else {
                        startLogin();
                    }
                }
            });

            holder.itemView.setLayoutParams(layoutParams);

//            } else {
//                holder.itemView.setVisibility(View.GONE);
//            }
        }

        @Override
        public int getItemCount() {
            return gamedata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView geme_tv;
            ImageView game_iv;

            MViewHolder(View itemView) {
                super(itemView);
                geme_tv = (TextView) itemView.findViewById(R.id.geme_tv);
                game_iv = (ImageView) itemView.findViewById(R.id.game_iv);

            }
        }
    }
}
